<?php
/**
 * 盘盈
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/4/16
 * Time: 17:29
 */
namespace App\Services\Warehouse;
class StockCountGainService extends PublicService{

    public function __construct()
    {
        parent::__construct(config('k3cloud.StockCountGain'));
    }
    /**
     * 保存
     * 1.formid：业务对象表单Id（必录）
     * 2.data：Json格式数据（详情参考Json格式数据）（必录）
     * 2.1.Creator：创建者名字（非必录）
     * 2.2.NeedUpDateFields：需要保存的字段,格式["fieldkey1","fieldkey2","entitykey1",...]，数组类型(非必录)
     * 2.3.NeedReturnFields：需要返回的结果字段,格式["fieldkey","entitykey.fieldkey",...]（非必录）
     * 2.4.IsDeleteEntry：是否删除分录，默认True删除（非必录）
     * 2.5.SubSystemId：菜单所在子系统Id（非必录）
     * 2.6.IsVerifyBaseDataField：是否验证所有的基础资料，默认False不验证（非必录）
     * 2.7.IsEntryBatchFill：是否批量填充分录：默认为Ture（非必录）
     * 2.8.Model：表单实体数据（必录）
     */
    public function SaveStockCountGain($arrdata=[],$is_submitandaudit=false){
        if(empty($arrdata['FID'])){
            $arrdata['FID']="0";
        }
        if(empty($arrdata['FBillNo'])){
            $arrdata['FBillNo']="";
        }
        $FOwnerTypeBDSupplier=config('k3cloud.FOwnerTypeBDSupplier');
        $FOwnerTypeBDOwnerOrg=config('k3cloud.FOwnerTypeBDOwnerOrg');
        $FOwnerIdHead=config('k3cloud.organization');
        $FOwnerTypeIdHead=$FOwnerTypeBDOwnerOrg;
        if($arrdata['k3_vmi']){
            $FOwnerTypeIdHead=$FOwnerTypeBDSupplier;
            $FOwnerIdHead=$arrdata['k3_supplier_sn'];
        }
        $goods=$this->PostGoodsData($arrdata['GoodsData']);
        $data=[
            "FID"=>$arrdata['FID'],
            "FStockCountInputNo"=>"",
            "FBillNo"=>$arrdata['FBillNo'],
            "FBillTypeID"=>[
                "FNumber"=>"PY01_SYS"
            ],
            "FStockOrgId"=>[
                "FNumber"=>config('k3cloud.organization')
            ],
            "FOwnerTypeIdHead"=>$FOwnerTypeIdHead,
            "FOwnerIdHead"=>[
                "FNumber"=>$FOwnerIdHead
            ],
            "FDate"=>date('Y-m-d H:i:s',time()),
            "FDeptId"=>[
                "FNumber"=>""
            ],
            "FStockerGroupId"=>[
                "FNumber"=>""
            ],
            "FStockerId"=>[
                "FNumber"=>""
            ],
            "FNoteHead"=>"",
            "FScanBox"=>"",
            "FBillEntry"=>$goods
        ];
        if($is_submitandaudit){
            $return=$this->save($data);
            if(empty($return['Id'])){
                return false;
            }
            $submitreturn=$this->SubmitStockCountGain([$return['Number']]);
            if(empty($submitreturn['Id'])){
                return false;
            }
            $auditreturn=$this->AuditStockCountGain([$return['Number']]);
            if(empty($auditreturn['Id'])){
                return false;
            }
            return $auditreturn;
        }else{
            return $this->save($data);
        }
    }

    private function PostGoodsData($GoodsData){
        if(!is_array($GoodsData)){
            return false;
        }
        $goods=array();
        $FOwnerTypeBDSupplier=config('k3cloud.FOwnerTypeBDSupplier');
        $FOwnerTypeBDOwnerOrg=config('k3cloud.FOwnerTypeBDOwnerOrg');
        foreach ($GoodsData as $k=>$v){
            $FOwnerFNumber=config('k3cloud.organization');
            if($v['k3_vmi']){
                $FOwnerTypeID=$FOwnerTypeBDSupplier;
                $FOwnerFNumber=$v['k3_supplier_sn'];
            }else{
                $FOwnerTypeID=$FOwnerTypeBDOwnerOrg;
            }
            $goods[]=[
                "FEntryID"=>null,
                "FMaterialId"=>[
                    "FNumber"=>$v['goods_sn']
                ],
                "FUnitID"=>[
                    "FNumber"=>$v['spec_unit']
                ],
                "FCountQty"=>$v['goods_number'],
                "FStockId"=>[
                    "FNumber"=>$v['StockNubmer']
                ],
                "FLOT"=>[
                    "FNumber"=>""
                ],
                "FBusinessDate"=>date('Y-m-d H:i:s',time()),
                "FOwnerTypeId"=>$FOwnerTypeID,
                "FOwnerid"=>[
                    "FNumber"=>$FOwnerFNumber
                ],
                "Fnote"=>null,
                "FStockStatusId"=>[
                    "FNumber"=>"KCZT01_SYS"
                ],
                "FBomId"=>[
                    "FNumber"=>""
                ],
                "FKeeperTypeId"=>"BD_KeeperOrg",
                "FKeeperId"=>[
                    "FNumber"=>config('k3cloud.organization')
                ],
                "FProduceDate"=>null,
                "FExpiryDate"=>null,
                "FExtAuxUnitId"=>[
                    "FNumber"=>""
                ],
                "FMtoNo"=>null,
                "FExtAuxUnitQty"=>0.0,
                "FExtSecAcctQty"=>0.0,
                "FExtSecGAINQty"=>0.0,
                "FProjectNo"=>null,
                "FServiceContext"=>null
            ];
        }
        return $goods;
    }

    /**
    提交
     *
     * 1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.CreateOrgId：创建者组织，字符串类型（非必录）
    2.2.Numbers：单据编号集合，格式["billNo1","billNo1",...]，数组类型（必录）
     */

    public function  SubmitStockCountGain($Numbers=[]){
        return $this->submit($Numbers);
    }

    /**
    审核
     * 1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.CreateOrgId：创建者组织，字符串类型（非必录）
    2.2.Numbers：单据编号集合，格式["billNo1","billNo1",...]，数组类型（必录）
     */
    public function AuditStockCountGain($Numbers=[]){
        return $this->audit($Numbers);
    }
}